package in.muton.panels.paginator.components.badges;

import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.user.UserCourses;
import in.muton.edushare.pages.user.UserDept;
import in.muton.edushare.pages.user.UserPage;
import in.muton.edushare.pages.user.UserUniv;
import in.muton.edushare.url.EntityPageParams;
import in.muton.panels.paginator.badgehelpers.user.BadgeRoles;
import in.muton.panels.paginator.helpers.RoleChanger;
import in.muton.panels.paginator.helpers.deletelinks.UserRemove;
import in.muton.panels.paginator.helpers.links.UserLink;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.Users;

public class UserBadge extends EdusharePanel {
	public static final Log log = LogFactory.getLog(UserBadge.class);

	private void addComponents(String id, EduShareBasePage page, IModel<Users> model) {
		userModel = model;
		add(CSSPackageResource.getHeaderContribution(UserBadge.class,
				"badge.css"));
		PageParameters p = EntityPageParams.getParams(model);

		Link<Void> l = new BookmarkablePageLink<Void>("imgLink",
				UserPage.class, p);
		add(l);
		l.add(new ProfileImage("img", model,
				ApplicationConstants.FILE_SUFFIX_ICON));
		
		add(l = new UserLink("nameLink", model, "name"));
		
		l.add(new SimpleAttributeModifier("title", model.getObject()
				.getCompleteUserName()));
		add(l);
		add(new BookmarkablePageLink<Void>("univ", UserUniv.class, p));
		add(new BookmarkablePageLink<Void>("dept", UserDept.class, p));
		add(new BookmarkablePageLink<Void>("course", UserCourses.class, p));
		add(new Label("role", new PropertyModel<String>(this, "roleStr")));
		add(new UserRemove("removeLink", model, bRole, this));
	}
	
	public String getRoleStr(){
		if (bRole == null)
			return "";
		return bRole.getRole(userModel.getObject());
	}

	public UserBadge(String id, EduShareBasePage page, IModel<Users> model){
		this(id, page, model, null);
	}
	
	public UserBadge(String id, EduShareBasePage page, IModel<Users> model, BadgeRoles br) {
		super(id, model, page);
		this.bRole = br;
		addComponents(id, page, model);

		Label invitedtag = new Label("invitedUser", "xxx");
		if (bRole == null || !bRole.isInvited(model.getObject())) {
			invitedtag.setVisible(false);
		}
		add(invitedtag);

		add(new RoleChanger("roleLV", br, model, this));
	}



	private IModel<Users> userModel = null;
	private BadgeRoles bRole = null;
	private static final long serialVersionUID = -5709398035770195656L;
}
